<template>
  <view class="login-content"> </view>
</template>

<script>
import mRequest from "@/api/home";
import hccmStore from "@/common/hccm-store.js";
import hccmConfig from "@/common/hccm-config.js";
export default {
  data() {
    return {
      action: "", //
      phone: "", //
      hospId: "",
      prescriptionIds: "",
    };
  },
  onLoad(options) {
    // 检测版本 bak
    let that = this;
    uni.showLoading({
      title: "正在加载...",
    });
    const updateManager = uni.getUpdateManager();
    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      console.log("请求完新版本信息的回调", res);
      if (!res.hasUpdate) {
        that.phone = options.phone || "";
        // that.phone = options.phone || '18613856809'
        // 18613856809   18613856809 18642609403 18018962168
        that.prescriptionIds = options.prescriptionId || "";
        // that.prescriptionIds = options.prescriptionId || '410'
        that.action = options.action || "list";
        that.hospId = options.hospId || "";
        // that.hospId = options.hospId || 'H21010200543'

        // 13470076278 测试地址的账号 13256094967
        if (that.phone) {
          hccmStore.setUserPhone(that.phone);
          that.getToken();
          if (that.hospId) {
            hccmStore.setUserHospId(that.hospId);
          }
        } else {
          that.toUrlByAction();
        }
      } else {
        uni.hideLoading();
        updateManager.onUpdateReady(function (res) {
          console.log("res", res);
          uni.showModal({
            title: "更新提示",
            content: "新版本已经准备好，是否重启应用？",
            success(res) {
              if (res.confirm) {
                // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                updateManager.applyUpdate();
              }
            },
          });
        });
      }
    });
  },
  methods: {
    // 获取版本
    getVersion() {},
    // 通过手机号获取token
    getToken() {
      let param = {
        phone: this.phone,
      };
      mRequest.getToken(param).then(
        (res) => {
          hccmStore.setPhoneToken(res.responseData);
          uni.login({
            success: (res) => {
              this.getOpenId(res.code);
            },
          });
        },
        (error) => {}
      );
    },
    // 获取openId
    getOpenId(code) {
      let param = {
        code: code,
      };
      mRequest.getOpenId(param).then(
        (res) => {
          hccmStore.setWxOpenId(res.responseData);
          this.getPatient();
          // if (this.action == 'order') {
          //   this.toUrlByAction()
          // } else {
          //   this.getPatient()
          // }
        },
        (error) => {}
      );
    },
    getPatient() {
      if (hccmStore.getUserPhone()) {
        let param = {
          phoneNo: hccmStore.getUserPhone(),
        };
        mRequest.getPatient(param).then((res) => {
          hccmStore.setUser(res.responseData);
          uni.hideLoading();
          this.toUrlByAction();
        });
      }
    },

    // TODO 需要判断进start页面得时候进哪个页面，现在暂时是home-main
    toUrlByAction() {
      //
      if (this.action == "order") {
        uni.redirectTo({
          //关闭当前页面，跳转到应用内的某个页面。
          url: `/pages/order/order?prescriptionId=${this.prescriptionIds}&hospId=${this.hospId}&action=${this.action}`,
        });
      } else if (this.action == "orderList") {
        console.log("action", this.action, this.phone);
        uni.redirectTo({
          //关闭当前页面，跳转到应用内的某个页面。
          url: `/pages/order-list/order-list?hospId=${this.hospId}`,
        });
      } else if (this.action == "list") {
        console.log("action", this.action, this.phone);
        uni.redirectTo({
          //关闭当前页面，跳转到应用内的某个页面。
          url: `/pages/home-main/home-main?hospId=${this.hospId}`,
        });
      } else {
        uni.redirectTo({
          //关闭当前页面，跳转到应用内的某个页面。
          url: `/pages/home-main/home-main`,
        });
      }
    },
  },
};
</script>

<style lang="scss">
.login-content {
  background-color: #fff;
}
</style>
